import { useMemo } from 'react';

import { getRoutesFromModules } from '@/router/utils';
import { useMenuIds } from '@/store/userStore';

/**
 * return routes about permission
 */
export function usePermissionRoutes() {
  const allRouters = getRoutesFromModules();
  const menuIds = useMenuIds();

  // 切换回静态路由
  return useMemo(() => {
    const routerArr: any = [];
    allRouters.forEach((item) => {
      if (item.children) {
        const arr = item.children.filter((sub) => {
          return !sub.meta || menuIds.includes(sub.meta.key);
        });
        if (arr.length > 1) {
          routerArr.push({ ...item, children: arr });
        }
      } else {
        routerArr.push(item);
      }
    });
    return routerArr;
  }, [menuIds]);
}
